package com.xxx.video.main.presenters;

import android.content.Context;

import androidx.annotation.NonNull;

import com.xxx.player.gsyvideoplayer.utils.NetworkUtils;
import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.main.beans.DynamicInfoBean;
import com.xxx.video.main.beans.LikeResultBean;
import com.xxx.video.main.beans.VideoDetailsBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class VideoDetailsFragmentPresenter extends BasePresenter<IVideoDetailsFragmentView> {

    public VideoDetailsFragmentPresenter(@NonNull Context context, IVideoDetailsFragmentView baseView) {
        super(context, baseView);
    }

    /**
     * get content info
     *
     * @param videoId video id
     */
    public void getContentInfo(String videoId) {
        NetWorkUtils.getInstance().getContentInfo(videoId, new CommonResponseListener<VideoDetailsBean>() {
            @Override
            public void onSuccess(VideoDetailsBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(3);
                }
            }
        });

    }

    /**
     * star
     *
     * @param videoId video id
     */
    public void fabulous(String videoId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        NetWorkUtils.getInstance().fabulous(videoId, new CommonResponseListener<LikeResultBean>() {

            @Override
            public void onSuccess(LikeResultBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().starSuccess(successOb.praise);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    /**
     * un like
     *
     * @param videoId video id
     */
    public void dislike(String videoId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        NetWorkUtils.getInstance().dislike(videoId, new CommonResponseListener<LikeResultBean>() {

            @Override
            public void onSuccess(LikeResultBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().unLikeSuccess(successOb.praise);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    public void downloadFollow(String videoId, String type) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        NetWorkUtils.getInstance().downloadFollow(videoId, type, new CommonResponseListener<BaseBean>() {

            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addDownloadSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    /**
     * favorite
     *
     * @param status  status
     * @param videoId videoId
     */
    public void favorite(boolean status, String videoId) {
        if (!UserManager.getInstance().isLogin()) {
            if (getBaseView() != null) {
                getBaseView().startLoginActivity();
            }
            return;
        }
        if (!status) {
            NetWorkUtils.getInstance().collection(videoId, String.valueOf(0), new CommonResponseListener<BaseBean>() {
                @Override
                public void onSuccess(BaseBean successOb, String result) {
                    if (ResponseUtils.getInstance().isNoError(successOb)) {
                        if (getBaseView() != null) {
                            getBaseView().collectionSuccess();
                        }
                    }
                }

                @Override
                public void onFailure(Object failureObj) {

                }
            });

        }else{
            if (getBaseView() != null) {
                ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_collection_yet));
                getBaseView().startFavoriteActivity();
            }
        }
    }
}
